<?php
namespace Admin\Controller;
use Think\Controller;
class FoodController extends BaseController {
  
 public function regfood(){
  	 $this->display();

  }     
public function foodlist(){
    $Foodsmodel=M('foods');
        $foods=$Foodsmodel->order('id asc')->select();
        $this->foods=$foods;
        //dump($food);
     $this->display();

  }  
public function delfoods($id){
   /*dump($_GET);
   
   echo $id;//如果只写上面这3行，注释部分，会再页面显示要删除的数据
     
  */
      
    $foodsModel=M('foods');
        $res=$foodsModel->where("id=$id")->find();
        $photoPath = realpath("./Public/Uploads/foodphoto/".$res['picture']);
        unlink($photoPath);
        //$res=$usersmodel->where("id=$id")->delete();
        if ($foodsModel->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }




}

Public function dofood(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']); 
        //dump($file);      
       
        $FoodModel = M('foods'); // 实例化User模型
        $FoodModel->foods_name =  I('post.foods_name'); // 设置用户的创建时间
        $FoodModel->intro =  I('post.intro');
        $FoodModel->tid =  I('post.tid');
        $FoodModel->uploaddate =  date("Y-m-d H:i:s"); // 设置用户的创建时间
         //dump($data);
             if ($file['picture']['size']==0) {//没有上传轮播图片
                        //$this->error('请上传背景图片！'); 
                        
                    }else{//有图片
                              
                            
                            $info=$this->MyUpload();
                            $FoodModel->picture = $info['picture']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$FoodModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('上传美食成功！', '../Food/foodlist');
                             }else{ 
                                    
                                     $this->error('数据错误'); 
                                   }

    
    }
 public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              //$upload->allowExts  = array('mp4','jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->exts  = array('jpg', 'gif', 'png', 'jpeg');
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Uploads/foodphoto/';// 设置附件上传目录

              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    } 

 public function detail($id){
        //dump($id);
        $foodsModel=M('foods');//实例化一个model对象
        $res=$foodsModel->where("id = $id")->select();//asc升序 desc降序
        $this->detail=$res;
        //dump($this->detail);
        $this->display();
    }




function editfood($id){
        $food = M('foods');
        $res = $food->where("id = $id")->find($data);
        $this->list=$res;
        $this->display();
    }
    function doUpdate($id){
   
        $foodsmodel=M('foods');
        if($_FILES['picture']['error'] == 0){
            $logoinfo = $this->MyUpload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['foods_name']=I('post.foods_name');
        $data['tid']=I('post.tid');
        $data['intro']=I('post.intro');
        $data['picture'] = $pname;
        $res=$foodsmodel->where("id = $id")->save($data);
        if (false!==$res){
            $this->success('信息修改成功','foodlist');
        } else{
            $this->error('信息修改失败');
        }
    }




        }
   

    
